package client;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import login.Login;

public class ClientMachine extends JFrame {

	private JPanel contentPane;
	JComboBox comboPort;
	JLabel lblPortServer;
	private JLabel lblServer;
	private JLabel lblLocalhost;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientMachine frame = new ClientMachine();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @throws IOException
	 * @throws UnknownHostException
	 */
	public ClientMachine() throws IOException {
		setTitle("Server Machine");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 220, 156);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lblPortServer = new JLabel("Port Server:");
		lblPortServer.setBounds(10, 43, 68, 14);
		contentPane.add(lblPortServer);
		String item[] = { "2014", "2015", "2016", "2017", "2018" };
		comboPort = new JComboBox(item);
		comboPort.setBounds(88, 40, 92, 20);
		contentPane.add(comboPort);

		lblServer = new JLabel("Server:");
		lblServer.setBounds(10, 18, 46, 14);
		contentPane.add(lblServer);

		lblLocalhost = new JLabel("localhost");
		lblLocalhost.setForeground(Color.RED);
		lblLocalhost.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblLocalhost.setBounds(88, 18, 92, 14);
		contentPane.add(lblLocalhost);

		JButton btnRequire = new JButton("Require");
		btnRequire.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int port = Integer.parseInt(comboPort.getSelectedItem()
						.toString());
				try {
					Socket client = new Socket("localhost", port);
					JOptionPane.showMessageDialog(ClientMachine.this,"Connect port "+client.getPort());
					Login l=new Login();
					l.setVisible(true);
					l.setTitle("From server "+port);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					JOptionPane.showMessageDialog(ClientMachine.this, "This port wasn't open");
				}
			}
		});
		btnRequire.setBounds(88, 71, 92, 23);
		contentPane.add(btnRequire);
		// /////////////////////////////////////

	}
	
}
